package com.tarena.lbs.activity.dao.mapper;


import com.tarena.lbs.activity.pojo.po.ActivityPO;
import com.tarena.lbs.activity.pojo.query.ActivityQuery;
import com.tarena.lbs.activity.pojo.vo.ActivityVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ActivityMapper {

    @Insert({
            "INSERT INTO activity (",
            " activity_name, start_date, end_date, describes, img_ids, shop_ids,",
            " target_customer, activity_target, delivery_channel, reward_coupon_id,",
            " share_profile, share_url, status, business_id, create_at, update_at,",
            " reward_type, enable_status, activity_type, channel_type, marketing_type, activity_json",
            ") VALUES (",
            " #{activityName}, #{startDate}, #{endDate}, #{describes}, #{imgIds}, #{shopIds},",
            " #{targetCustomer}, #{activityTarget}, #{deliveryChannel}, #{rewardCouponId},",
            " #{shareProfile}, #{shareUrl}, #{status}, #{businessId}, #{createAt}, #{updateAt},",
            " #{rewardType}, #{enableStatus}, #{activityType}, #{channelType}, #{marketingType}, #{activityJson}",
            ")"
    })
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int save(ActivityPO activityPO);

    @Update({
            "UPDATE activity SET",
            " activity_name = #{activityName},",
            " start_date = #{startDate},",
            " end_date = #{endDate},",
            " describes = #{describes},",
            " img_ids = #{imgIds},",
            " shop_ids = #{shopIds},",
            " target_customer = #{targetCustomer},",
            " activity_target = #{activityTarget},",
            " delivery_channel = #{deliveryChannel},",
            " reward_coupon_id = #{rewardCouponId},",
            " share_profile = #{shareProfile},",
            " share_url = #{shareUrl},",
            " status = #{status},",
            " reward_type = #{rewardType},",
            " enable_status = #{enableStatus},",
            " update_at = #{updateAt},",
            " activity_json = #{activityJson},",
            " marketing_type = #{marketingType}",
            " WHERE id = #{id}"
    })
    int update(ActivityPO activityPO);

    @Select({
            "SELECT",
            " id AS id, activity_name AS activityName, start_date AS startDate, end_date AS endDate,",
            " describes AS describes, img_ids AS imgIds, shop_ids AS shopIds, target_customer AS targetCustomer,",
            " activity_target AS activityTarget, delivery_channel AS deliveryChannel, reward_coupon_id AS rewardCouponId,",
            " share_profile AS shareProfile, share_url AS shareUrl, status AS status, business_id AS businessId,",
            " create_at AS createAt, update_at AS updateAt, reward_type AS rewardType, enable_status AS enableStatus,",
            " activity_type AS activityType, channel_type AS channelType, marketing_type AS marketingType, activity_json AS activityJson",
            " FROM activity WHERE id = #{id}"
    })
    ActivityPO getDetailById(@Param("id") Integer id);

    @Select({
            "<script>",
            "SELECT",
            " id AS id, activity_name AS activityName, start_date AS startDate, end_date AS endDate,",
            " describes AS describes, img_ids AS imgIds, shop_ids AS shopIds, target_customer AS targetCustomer,",
            " activity_target AS activityTarget, delivery_channel AS deliveryChannel, reward_coupon_id AS rewardCouponId,",
            " share_profile AS shareProfile, share_url AS shareUrl, status AS status, business_id AS businessId,",
            " create_at AS createAt, update_at AS updateAt, reward_type AS rewardType, enable_status AS enableStatus,",
            " activity_type AS activityType, channel_type AS channelType, marketing_type AS marketingType, activity_json AS activityJson",
            " FROM activity",
            " <where>",
            "  <if test=\"businessId != null\"> AND business_id = #{businessId} </if>",
            " </where>",
            " ORDER BY id DESC",
            " LIMIT #{offset}, #{pageSize}",
            "</script>"
    })
    List<ActivityVO> selectPage(@Param("businessId") Integer businessId,
                                @Param("offset") int offset,
                                @Param("pageSize") int pageSize);

    @Select({
            "<script>",
            "SELECT COUNT(1) FROM activity",
            " <where>",
            "  <if test=\"businessId != null\"> AND business_id = #{businessId} </if>",
            " </where>",
            "</script>"
    })
    Long count(@Param("businessId") Integer businessId);

    @Select({
            "SELECT",
            " id AS id, activity_name AS activityName, start_date AS startDate, end_date AS endDate,",
            " describes AS describes, img_ids AS imgIds, shop_ids AS shopIds, target_customer AS targetCustomer,",
            " activity_target AS activityTarget, delivery_channel AS deliveryChannel, reward_coupon_id AS rewardCouponId,",
            " share_profile AS shareProfile, share_url AS shareUrl, status AS status, business_id AS businessId,",
            " create_at AS createAt, update_at AS updateAt, reward_type AS rewardType, enable_status AS enableStatus,",
            " activity_type AS activityType, channel_type AS channelType, marketing_type AS marketingType, activity_json AS activityJson",
            " FROM activity WHERE shop_ids LIKE CONCAT('%', #{storeId}, '%')"
    })
    List<ActivityPO> getActivitiesByStoreId(@Param("storeId") String storeId);
}
